package com.zhangh.summer.admin.support.ds.common;

import com.zhangh.summer.admin.support.ds.common.setting.DsSettingProperties;
import com.zhangh.summer.admin.support.ds.es.EsClientFactory;
import com.zhangh.summer.admin.support.ds.mysql.MybatisMapperFactory;
import com.zhangh.summer.admin.support.ds.mysql.MysqlSelector;
import com.zhangh.summer.admin.support.ds.redis.RedisClient;
import com.zhangh.summer.admin.support.ds.rocketmq.RocketMQProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @description: TODO
 * @author: zhanghui  zhanghlucky@163.com
 * @create: 2020-03-20 16:50
 **/
@Component
@Slf4j
public class MixedDSImpl  implements MixedDS  {

    @Autowired
    private DsSettingProperties dsSettingProperties;
    @Autowired
    private Environment env;

    private final static Map<String, RedisClient> REDIS_CLIENT_MAP =new ConcurrentHashMap();

    private final static Map<String, EsClientFactory> ES_CLIENT_MAP = new ConcurrentHashMap<>();

    private RocketMQProducer rocketMQProducer;


    /**
     * 默认的mysql数据源
     * @return mysql
     */
    public MysqlSelector mysql(){
        return MysqlSelector.getInstance();
    }

    /**
     * 事务数据源
     * @return
     */
    @Override
    public MybatisMapperFactory txDs() {
        return MybatisMapperFactory.getInstance();
    }

    public RedisClient redis(){
        return this.redis(dsSettingProperties.getDefaultRedisName());
    }
    public RedisClient redis(String redisName){
        if (StringUtils.isNotBlank(redisName)){
            RedisClient redisClient=REDIS_CLIENT_MAP.get(redisName);
            if (null==redisClient){
                synchronized (this){
                    redisClient=new RedisClient(redisName);
                    REDIS_CLIENT_MAP.put(redisName,redisClient);
                }
            }
            return redisClient;
        }else{
            log.error("redisName can not be null! please check method params or ds.setting.default.redisName in properties");
            return null;
        }
    }

    /**
     * 使用默认es数据源
     * @return es
     */
    public EsClientFactory es(){
        return es(dsSettingProperties.getDefaultEsName());
    }

    /**
     * 使用指定es数据源
     * @param esName esname
     * @return es
     */
    public EsClientFactory es(String esName){
        if (StringUtils.isNotBlank(esName)){
            EsClientFactory esClient=ES_CLIENT_MAP.get(esName);
            if (null!=esClient){
                return esClient;
            }
            synchronized (this){
                esClient=new EsClientFactory(esName);
                ES_CLIENT_MAP.put(esName,esClient);
            }
            return esClient;
        }else{
            log.error("esName can not be null! please check method params or ds.setting.default.esName in properties");
            return null;
        }
    }

    @Override
    public RocketMQProducer rocketMQProducer() {
        if (null!=rocketMQProducer){
            return rocketMQProducer;
        }
        synchronized (this){
            rocketMQProducer=new RocketMQProducer();
        }
        return rocketMQProducer;
    }


    public Map<String, RedisClient> getRedisClientMap(){
        return REDIS_CLIENT_MAP;
    }


}
